﻿/*
 * ztime_unittest.cxx
 *
 *  Created on: 2015年4月9日
 *      Author: Fifi Lyu
 */

#include <gtest/gtest.h>
#include "zpsoe/zalgorithm/ztime.h"
#include "zpsoe/zalgorithm/zstring.h"
#include <sys/timeb.h>

using namespace zpsoe::zalgorithm;

TEST(ZtimeTest, mwait) {
    const time_t sec_ = 1;
    time_t start_, end_;

    time(&start_);
    ztime::mwait(sec_);
    time(&end_);

    const double diff_ = difftime(end_, start_);

    // diff_ >= sec_
    EXPECT_GE(diff_, sec_);
}

TEST(ZtimeTest, msleep) {
#ifdef PLATFORM_LINUX
    typedef struct timeb _timeb;
    #define _ftime(timebuf) ftime(timebuf);
#endif

    const time_t milli_sec_ = 1100;
    _timeb start_;
    _timeb end_;

    _ftime(&start_);
    ztime::msleep(milli_sec_);
    _ftime(&end_);

    const time_t diff_ =
            (1000 * (end_.time - start_.time) + (end_.millitm - start_.millitm));

    // diff_ >= milli_sec_
    EXPECT_GE(diff_, milli_sec_);
}

TEST(ZtimeTest, cmp_date) {
    cmp_t ret_ = ztime::cmp_date("2015-11-16 14:06:36", "2015-04-09 00:11:36");
    EXPECT_EQ(CMP_GREATER,ret_);
}
